package com.linked.basemanage.switchsetting.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linked.basemanage.base.bean.param.SwitchSettingPageParam;
import com.linked.basemanage.base.bean.po.SwitchInfoPO;
import com.linked.basemanage.switchsetting.service.ISwitchSettingService;
import com.linked.universal.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :dbq
 * @date : 2022/11/23 9:32
 * 开发设置控制类
 */

@RestController
@RequestMapping("switchsetting")
public class SwitchSettingController {

    private final static Logger logger = LoggerFactory.getLogger(SwitchSettingController.class);

    private final ISwitchSettingService switchSettingService;

    @Autowired
    public SwitchSettingController(ISwitchSettingService switchSettingService) {
        this.switchSettingService = switchSettingService;
    }

    @PostMapping("querySwtichSettingPage")
    Result querySwtichSettingPage(@RequestBody SwitchSettingPageParam param) {
        IPage<SwitchInfoPO> pageResutl = null;
        try {
            pageResutl = switchSettingService.querySwtichSettingPage(param);
        } catch (Exception e) {
            logger.error("查询开关设置页面接口异常", e);
            Result.error();
        }
        return Result.success(pageResutl);
    }

    @PostMapping("saveSwitchInfo")
    Result saveSwitchInfo(@RequestBody SwitchInfoPO param) {

        boolean ret = false;
        try {
            ret = switchSettingService.saveSwitchInfo(param);
        } catch (Exception e) {
            logger.error("保存开关设置接口异常！", e);
        }
        return ret ? Result.ok(true, "保存成功") : Result.ok(false, "保存失败");
    }

    @PostMapping("querySwitchList")
    Result querySwitchList(@RequestBody SwitchInfoPO param) {
        List<SwitchInfoPO> switchList = null;
        try {
            switchList = switchSettingService.querySwitchList(param);
        } catch (Exception e) {
            logger.error("查询开关列表接口异常！", e);
            return Result.error("查询开关列表接口异常！");
        }
        return Result.success(switchList);
    }

    @PostMapping("turnSwitch")
    Boolean turnSwitch(@RequestBody String switchName) throws Exception {
        logger.info("入参：" + switchName);
        SwitchInfoPO temp = switchSettingService.findSwitchByName(switchName);
        if (temp == null) {
            return null;
        }
        temp.setIfSwitch(temp.getIfSwitch() == 1 ? 0 : 1);
        boolean ret = switchSettingService.updateSwitchInfo(temp);
        return ret ? true : false;
    }

    @PostMapping("updateSwitchInfo")
    Boolean updateSwitchInfo(@RequestBody SwitchInfoPO param) throws Exception {
        logger.info("入参：" + param.getSwitchName());
        boolean ret = switchSettingService.updateSwitchInfo(param);
        return ret ? true : false;
    }


}
